﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace LogBase
{
    public class Comment
    {
        /// <summary>
        /// 获取备注信息列表
        /// </summary>
        /// <param name="sitename">站点ID</param>
        /// <returns>DataTable</returns>
        public static DataTable getPageCommentList(string sitename)
        {
            string sql = "select a.[guid],b.[sitename],a.[uri],a.[comment] from [QT_PageComment] a left join [QT_SiteList] b on b.[guid]=a.[sitename] where b.[guid]='" + sitename + "' ";
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            return dt;
        }

        /// <summary>
        /// 添加页面备注
        /// </summary>
        /// <param name="sitename">站点ID</param>
        /// <param name="url">页面地址</param>
        /// <param name="comment">备注信息</param>
        /// <returns>int</returns>
        public static int AddPageComment(string sitename, string url, string comment)
        {
            if (Exsits(null, sitename, url)) return -2;
            string sql = "insert into [QT_PageComment]([sitename],[uri],[comment]) values('" + sitename + "','" + url + "','" + comment + "')";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sitename"></param>
        /// <param name="url"></param>
        /// <param name="comment"></param>
        /// <returns></returns>
        public static int UpdatePageComment(string guid, string sitename, string url, string comment)
        {
            if (Exsits(guid, sitename, url)) return -2;
            string sql = "update [QT_Pagecomment] set [comment]='" + comment + "' where [sitename]='" + sitename + "' and [uri]='" + url + "'";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        /// <summary>
        /// 删除页面备注信息
        /// </summary>
        /// <param name="sitename">站点ID</param>
        /// <param name="urls">页面URL</param>
        /// <returns>int</returns>
        public static int DeletePageComment(string sitename, List<string> urls)
        {
            string sql = "delete from [QT_Pagecomment] where [sitename]='" + sitename + "' and [uri] in('" + string.Join("','", urls.ToArray()) + "')";
            int rval = new QT.DBO.SqlDBHelper(MyDB.ConnString).ExecuteNonQuery(sql);
            return rval;
        }

        public static bool Exsits(string guid, string sitename, string url)
        {
            string sql = "select [guid] from [QT_PageComment] where [sitename]='" + sitename + "' and [uri]='" + url + "'" + (string.IsNullOrEmpty(guid) ? "" : (" and [guid]<>'" + guid + "'"));
            DataTable dt = new QT.DBO.SqlDBHelper(MyDB.ConnString).DataTable(sql);
            if (dt.Rows.Count > 0) return true;
            else return false;
        }
    }
}
